package com.example.qa.adapter.in.web.demos.web.controller;

import com.example.qa.adapter.in.web.demos.web.controller.dto.CreateQaRequestDTO;
import com.example.qa.service.application.command.CreateQaCommand;
import com.example.qa.service.application.port.in.*;
import com.example.qa.adapter.in.web.demos.web.dto.QasUpdateDto;
import com.example.qa.service.application.command.UpdateQasCommand;
import com.example.qa.service.domain.Choice;
import com.example.qa.service.domain.Qa;
import com.example.qa.service.domain.valueobject.QaId;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/qa")
@RestController
@RequiredArgsConstructor
public class QaController {
    private final GetQaListUseCase getQaListUseCase;
    private final UpdateQasUseCase updateQasUseCase;
    private final DeleteOneQaUseCase deleteOneQaUseCase;
    private final GetQaoneUseCase getQaoneUseCase;

    private final DeleteAllQasUseCase deleteAllQasUseCase;
    private final CreateQaCase createQaCase;
    private final GetChatContentCase getChatContentCase;
    /**
     * author: yeguiyang
     * 获取所有问答（）
     * @return
     */
    @GetMapping("/get")
    public List<Qa> getQas()
    {
        log.info("getQas");
        return getQaListUseCase.getQas();
    }
    /**
     * author: zhangyi
     * 新增问答
     * @return
     */
    @PostMapping("/add")
    public Qa createQa(@RequestBody CreateQaRequestDTO createQaRequestDTO){
        CreateQaCommand command= CreateQaCommand.builder()
                .question(createQaRequestDTO.question())
                .answer(createQaRequestDTO.answer())
                .build();
        return createQaCase.createQa(command);
    }

    /**
     * author: 冯楷晟
     * 根据id更新问答
     * @return
     */
    @PostMapping("/update")
    public void updateQas(@RequestBody QasUpdateDto qasUpdateDto)
    {
        log.info("updateQas");
        UpdateQasCommand  updateQasCommand=UpdateQasCommand.builder()
                .id(qasUpdateDto.getId())
                .question(qasUpdateDto.getQuestion())
                .answer(qasUpdateDto.getAnswer())
                .build();
         updateQasUseCase.updateQas(updateQasCommand);
    }


    /**
     * author: liwenchao
     * 获取单个问答
     * @return
     */
    @GetMapping("/getone/{id}")
    public Qa getQa(@PathVariable("id") long id)
    {
        log.info("getQa");
        return getQaoneUseCase.getQa( id);
    }

    @GetMapping("/getChatContent")
    public List<Choice> getChatContent(@RequestParam("question") String question)
    {
        log.info("getChatContent");
        return getChatContentCase.getChatContent(question);
    }

    /**
     * author: suyucheng
     * 删除所有问答
     */
    @DeleteMapping("/delete-all")
    public void deleteAllQas()
    {
        log.info("deleteAllQas");
        deleteAllQasUseCase.deleteAllQas();
    }
    /**
     * author: wujiajun
     * 删除单个数据
     * @return
     */
    @DeleteMapping("/deletebyid/{id}")
    public void deleteOneQa(@PathVariable("id") Long id)
    {
        log.info("deleteOneQa");
        deleteOneQaUseCase.deleteOneQa(id);
    }

}
